package com.matrix.yukun.matrix.tool_module.battery;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.matrix.yukun.matrix.R;
import com.matrix.yukun.matrix.chat_module.mvp.MVPBaseActivity;
import com.matrix.yukun.matrix.main_module.utils.SPUtils;
import com.matrix.yukun.matrix.selfview.BatteryLinearLayout;
import com.matrix.yukun.matrix.selfview.PercentCircle;
import com.matrix.yukun.matrix.tool_module.battery.presenter.BatteryContract;
import com.matrix.yukun.matrix.tool_module.battery.presenter.BatteryPresenter;
import com.matrix.yukun.matrix.util.OperationCountdownTimer;
import com.matrix.yukun.matrix.util.log.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 */
public class BatteryChargeActivity extends MVPBaseActivity<BatteryPresenter> implements OperationCountdownTimer.Callback, BatteryContract.View {

    BatteryReceiver mBatteryReceiver;
    OperationCountdownTimer mCountdownTimer;
    Intent mIntent;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.bll_progress)
    BatteryLinearLayout bllProgress;
    @BindView(R.id.percent_circle)
    PercentCircle percentCircle;
    @BindView(R.id.tv_category)
    TextView tvCategory;
    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.tv_logcat)
    TextView tvLogcat;
    private BatteryManager mManager;
    private int originPercent;
    private double mBatteryCapacity;
    private int second;
    private double perSecondmA = 0.1;
    private boolean startSava;
    private boolean isCharge;

    public static void start(Context context) {
        Intent intent = new Intent(context, BatteryChargeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected BatteryPresenter createPresenter() {
        return new BatteryPresenter(this, this);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_battery_charge;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        registerBroadcast();
        mCountdownTimer = new OperationCountdownTimer(1000 * 60 * 60, 2000, this);
    }

    /**
     * 注册广播
     */
    private void registerBroadcast() {
        // 注册电量广播
        mBatteryReceiver = new BatteryReceiver();
        registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    public void initDate() {
        mBatteryCapacity = mPresenter.getBatteryCapacity(this);
        originPercent = mPresenter.getBatteryPercent(this);
        float battery = SPUtils.getInstance().getBattery();
        if (battery > 0) {
            perSecondmA = battery;
        }
        updateBattery();
        mCountdownTimer.start();
    }

    private void updateBattery() {
        int currentCapacity = mPresenter.getExactBattery(this);
        int percent = mPresenter.getBatteryPercent(this);
        if (isCharge) {
            if (originPercent != percent && second > 0) {
                //跳过第一次计数值
                if (startSava) {
                    //每 2 second新增mA数
                    perSecondmA = mBatteryCapacity * 0.01 / second;
                    SPUtils.getInstance().setBattery((float) mPresenter.numberFormat(perSecondmA));
                }
                //刷新数据
                originPercent = percent;
                second = 0;
                startSava = true;
            } else {
                second++;
            }
        }
        //自动新增数值
        bllProgress.setMax((int) mBatteryCapacity);
        bllProgress.setProgress(mPresenter.numberFormat(currentCapacity + perSecondmA * second));
        if (currentCapacity + perSecondmA * second > mBatteryCapacity) {
            tvCategory.setText(mBatteryCapacity + "mA/" + mBatteryCapacity + "mA");
        } else {
            tvCategory.setText(mPresenter.numberFormat(currentCapacity + perSecondmA * second) + "mA/" + mBatteryCapacity + "mA");
        }
        percentCircle.setTargetPercent(percent);
        tvSecond.setText(mPresenter.numberFormat(perSecondmA) + "mA");
        String logcat = "perSecondmA：" + perSecondmA + "\n" + "startSava：" + startSava + "\n" + "second：" + second;
        tvLogcat.setText(logcat);
    }

    @Override
    public void initListener() {
        tvCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                second = 200;
                SPUtils.getInstance().setBattery(0.02f);
                originPercent--;
            }
        });
    }

    @Override
    public void onTick(long millisUntilFinished) {
        updateBattery();
    }

    @OnClick(R.id.iv_back)
    public void onClick(View view) {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 电池电量广播接受者
     */
    class BatteryReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取电量
            mIntent = intent;
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) {
//                percentCircle.setTargetPercent();
                isCharge = true;
            } else {
                isCharge = false;
            }
        }
    }


    @Override
    public void onCountdownFinish() {
    }

    /**
     * 释放资源
     */
    @Override
    protected void onDestroy() {
        // 释放资源 : 先释放子类资源
        if (mBatteryReceiver != null) {
            unregisterReceiver(mBatteryReceiver);
        }
        if (mCountdownTimer != null) {
            mCountdownTimer.release();
        }
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onDestroy();
    }
}